let connection = require("../dbutil/dbutil");
let myModule = require("./my-module");
let moment = require("moment");//引入日期格式类库

function getInfo(uid, token, callback) {
    let sql = "select * from user where uid = ? and token = ?";
    let sqlparams=[uid, token]
    connection.query(sql, sqlparams, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1002,
                data: []
            }
            if (result.length > 0 && myModule.verifyToken(token)) {
                let usql = "select * from payment_view where uid = '" + uid + "'";
                connection.query(usql, function (err, result) {
                    if (err) {
                        console.log(err);
                    } else {
                        ret.code = 1001
                        ret.data = result
                        callback(ret)
                    }
                })
            } else {
                callback(ret)
            }
        }
    })
}

function updateAddress(uid, token, address, callback) {
    let sql = "select * from user where uid = ? and token = ?";
    let sqlparams=[uid, token]
    connection.query(sql, sqlparams, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1002,
                msg: "失败"
            }
            if (result.length > 0 && myModule.verifyToken(token)) {
                let usql = "UPDATE user SET address = '" + address + "' WHERE uid = '" + uid + "'";;
                connection.query(usql, function (err) {
                    if (err) {
                        console.log(err);
                    } else {
                        ret.code = 1001
                        ret.msg = "成功"
                        callback(ret)
                    }
                })
            } else {
                callback(ret)
            }
        }
    })
}

function goPay(uid, token, oid, money, pwd, callback) {
    let sql = "select * from user where uid = ? and token = ?";
    let sqlparams=[uid, token]
    connection.query(sql, sqlparams, function (err, result) {
        if (err) {
            console.log(err);
        } else {
            let ret = {
                code: 1005,
                msg: "失败"
            }
            if (result.length > 0 && myModule.verifyToken(token)) {
                if (result[0].address.length > 0) {
                    if (result[0].password === pwd) {
                        let wsql = "select wmoney from wallet where uid = '" + uid + "'";
                        connection.query(wsql, function (err, result) {
                            if (err) {
                                console.log(err);
                            } else {
                                if (parseInt(result[0].wmoney) > parseInt(money)) {
                                doPay(oid, result[0].wmoney) 
                                let wmoney = parseInt(result[0].wmoney) - parseInt(money)
                                let upsql = "UPDATE wallet SET wmoney = '" + wmoney + "' WHERE uid = '" + uid + "'"; 
                                connection.query(upsql, function (err) {
                                        if (err) {
                                            console.log(err);
                                        } else {
                                            ret.code = 1001
                                            ret.msg = "成功"
                                            callback(ret)
                                        }
                                    })
                                } else {
                                    ret.code = 1002
                                    ret.msg = "余额不足"
                                    callback(ret)
                                }
                            }
                        })
                        
                    } else {
                        ret.code = 1003
                        ret.msg = "密码错误"
                        callback(ret)
                    }
                } else {
                    ret.code = 1004
                    ret.msg = "地址不可为空"
                    callback(ret)
                }
            } else {
                callback(ret)
            }
        }
    })
}

function doPay(oid, wmoney) {
    let oids = oid.split("$")
    let money = wmoney
    for (let i = 0; i < oids.length; i++) {
        let sql = "select * from payment_view where oid = '" + oids[i] + "'";
        connection.query(sql, function (err, result) {
            if (err) {
                console.log(err);
            } else {
                money -= result[0].pprice
                let time = moment().format('YYYY-MM-DD HH:mm:ss')
                let price = "-" + result[0].pprice
                let remark = "购买" + result[0].pname + " " + result[0].pversion + " " + result[0].pcolor
                let insql = "INSERT INTO `trade` (`uid`, `tmoney`, `type`, `ttime`, `remark`, `balance`) VALUES (?, ?, ?, ?, ?, ?)"
                let inSqlParams = [result[0].uid, price, "购买" , time, remark, money];
                connection.query(insql, inSqlParams, function (err, result) {
                    if (err) {
                        console.log(err)
                    }
                })
                let usql = "UPDATE `order` SET situation = '待收货' WHERE oid = '" + oids[i] + "'"; 
                connection.query(usql, inSqlParams, function (err, result) {
                    if (err) {
                        console.log(err)
                    }
                })
                let tsql = "DELETE FROM trolley WHERE uid =? AND pid = ?;";
                let tsqlparams=[oids[i].uid, oids[i].pid]
                connection.query(tsql, tsqlparams, function (err) {
                    if (err) {
                        console.log(err);
                    }
                })
            }
        })
    }
}

exports.getInfo = getInfo
exports.updateAddress = updateAddress
exports.goPay = goPay